#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 405; 
int n,a[N],ord[N],t[N],ans;
bool vis[N];
vector<int> g[N];
int ask()
{
	int res = 0;
	memcpy(t,a,sizeof a);
	for(int i = 1;i<=n;i++)
	{
		res+=t[ord[i]];
		for(auto &v:g[ord[i]])
			t[v]+=t[ord[i]];
	}
	return res;
}
void dfs(int x)
{
	if(x>n) return ans = max(ans,ask()),void();
	for(int i = 1;i<=n;i++)
		if(!vis[i])
			ord[x] = i,vis[i] = 1,dfs(x+1),vis[i] = 0;
}
signed main()
{
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i = 1;i<=n;i++)
		cin>>a[i];
	for(int i = 1,x,y;i<n;i++)
		cin>>x>>y,g[x].push_back(y),g[y].push_back(x);
	if(n<=10) dfs(1),cout<<ans,exit(0);
	return 0;
}

